<style lang="less" scoped>

  .el-row {
    margin-bottom: 20px;
  }

  .el-col {
    border-radius: 4px;
  }

  .bg-purple-dark {
    background: #ffffff;
  }

  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }

  .el-form-item {
    margin: 0px 20px 15px 20px;
  }

  .f-bs1 {
    border: 1px solid #dfe6ec;
  }

  .el-collapse {
    border: 0px;
    .el-collapse-item {
      border: 1px solid #dfe6ec;
      margin-bottom: 10px;
      .el-collapse-item__header {
        font-weight: bold;
        .el-collapse-item__header__arrow {
          display: none;
        }
      }
      .el-collapse-item__content {
        padding-bottom: 0px;
      }
      .el-collapse-item__wrap {
        border-bottom: 0px;
      }
    }
  }

  label {
    width: 100px;
  }

  .el-form-item[data-v-7da61f17] {
    margin: 0 0 0 0
  }

  .f-mt10 {
    margin-top: 10px;
  }

  .f-ml20 {
    margin-left: 20px;
  }

  .f-mr20 {
    margin-right: 20px;
  }

  .f-mb10 {
    margin-bottom: 10px;
  }

  .f-tar {
    text-align: right;
  }

  .f-h30 {
    height: 30px;
  }

  .f-c-red {
    color: red;
  }

  .el-radio-group {
    width: 60%;
  }

  .el-radio-invoice {
    margin-right: 30px;
  }

  .f-color {
    color: #25baaf;
  }

  .el-textarea {
    width: 86%;
  }

  .f-mb {
    margin-bottom: 0;
  }

  .f-fr {
    float: right;
  }

  .f-fl {
    float: left;
  }

  .f-lh38 {
    line-height: 38px;
  }

  .el-select {
    width: 100%;
  }

  .f-w233 {
    width: 100%;
  }

  .el-form-item__label {
    width: 150px;
  }

  .el-date-editor.el-input {
    width: 100%;
  }

  li {
    list-style-type: none;
  }

  .color-c8c8c8 {
    color: #c8c8c8;
  }

  .f-mt {
    margin-top: 0;
  }

  .width70 {
    width: 82%;
  }

</style>

<template>
  <section>
    <!--工具条-->
    <search-bar more-display>
      <el-row>
        <el-col :span="8">
          <el-form-item label=配置名称>
            <el-input
                      v-model="searchStmParamCnName" placeholder="请输入配置名称"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="系统编码">
            <el-input
                      v-model="searchStmParamType" placeholder="请输入系统编码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="配置值">
            <el-input type="number"
                      v-model.number="searchStmParamVal" placeholder="请输入配置值"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </search-bar>
    <el-col :span="24" style="margin-bottom: 10px">
      <el-button type="primary" @click="handleAdd">新增</el-button>
      <el-button type="primary" @click="myReset" style="float: right">重置</el-button>
      <el-button type="primary" @click="searchStmParam" style="float: right">查询</el-button>

    </el-col>

    <!--列表-->
    <el-table :data="tableData" highlight-current-row border @sort-change="sortChange" max-height="650"
              @selection-change="selsChange" v-loading="listLoading" style="width: 100%;">
      <el-table-column type="index" label="序号" width="60">
      </el-table-column>
      <el-table-column prop="statusName" label="状态" width="80" >
      </el-table-column>
      <el-table-column prop="stmParamCnName" label="配置名称" width="250" >
      </el-table-column>
      <el-table-column prop="stmParamType" label="系统编码" width="300" >
      </el-table-column>
      <el-table-column prop="stmParamVal" label="配置值" width="120" >
      </el-table-column>
      <el-table-column prop="scopeName" label="适用范围" width="120" >
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="remark" label="备注" >
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button type="success" v-if="scope.row.status===111002002"
                     @click="handleStart(scope.$index, scope.row)">启用

          </el-button>
          <el-button type="warning" v-if="scope.row.status===111002001"
                     @click="handleStop(scope.$index, scope.row)">停用

          </el-button>
          <el-button  type="primary" @click="getEditBYId(scope.$index, scope.row)">编辑 </el-button>
          <el-button type="danger"  @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total,sizes,prev, pager, next, jumper"
        :total="total" style="float: right">
      </el-pagination>
    </el-col>
    <!--新增 弹窗 -->
    <el-dialog :title="dialogTitle" size="tiny" :visible.sync="stmParamVisible" :close-on-click-modal="false">
      <el-form :model="stmParam" :rules="rules" label-width="110px" ref="stmParam">

        <el-row class="f-mt10 f-mb10">
          <el-col :span="24">
            <el-form-item label="配置名称" class="width70" prop="stmParamCnName">
              <el-input v-model="stmParam.stmParamCnName" placeholder="请输入配置名称 "></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row class="f-mt10 f-mb10">
          <el-col :span="24">
            <el-form-item label="系统编码" class="width70" prop="stmParamType">
              <el-input v-model="stmParam.stmParamType" placeholder="请输入系统编码"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row class="f-mt10 f-mb10">
          <el-col :span="24">
            <el-form-item label="配置值" class="width70" prop="stmParamVal"
                          :rules="[{required: true,  message: '请输入配置值', trigger: 'change'}]">
              <el-input type="number" v-model="stmParam.stmParamVal" placeholder="请输入配置值"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row class="f-mt10 f-mb10">
          <el-col :span="24">
            <el-form-item label="适用范围" class="width70" prop="scope">
              <el-select  @change="scopeChange" v-model="stmParam.scope"
                         placeholder="请选择">
                <el-option
                  v-for="item in $store.getters.SYSTEM_SCOPE"
                  :key="item.dictVal"
                  :label="item.cnDictValname"
                  :value="item.dictVal">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="f-mt10 f-mb10">
          <el-col :span="24">
            <el-form-item label="隶属机构" class="width70" prop="organizationId1" v-if="stmParam.scope === 110006002">
              <el-cascader
                placeholder="请选择隶属机构"
                :options="$store.state.initOrganizations"
                :props="{label: 'name',value: 'id'}"
                change-on-select
                v-model="stmParam.organizationId1"
              ></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row class="f-mt10 f-mb10">
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input type="textarea" v-model="stmParam.remark" placeholder="请输入备注"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="stmParamVisible = false">取消</el-button>
        <el-button type="primary" @click.native="handleSubmit" :loading="submitLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import {
  searchStmParam,
  saveStmParam,
  delStmParam,
  batchStart
} from '../../api/v3/stmParam.js';
import {getOrganizationVoById} from '../../api/v3/Organization';
export default {
  data () {
    return {
      currentOrganizationIds: [],
      dialogTitle: '新增',
      condition: {
        name: '',
        number: ''
      },
      searchStmParamType: '',
      searchStmParamCnName: '',
      searchStmParamVal: '',
      sels: [],
      tableData: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      sortProp: null,
      sortOrder: 'ascending',
      listLoading: false,
      stmParamVisible: false,
      submitLoading: false,
      // 表单数据
      stmParam: {
        'id': null,
        'stmParamCnName': null,
        'stmParamType': null,
        'stmParamVal': null,
        'scope': null,
        'organizationId1': [],
        'organizationId': null,
        'remark': null

      },
      // 验证规则
      rules: {
        scope: [
          {required: true, type: 'number', message: '请选择适用范围', trigger: 'change'}
        ],
        stmParamCnName: [
          {required: true, message: '请输入配置名称', trigger: 'blur'},
          {min: 2, max: 60, message: '长度在2到 60 个字符', trigger: 'blur'}
        ],
        organizationId1: [
          {type: 'array', required: true, message: '请选择隶属机构', trigger: 'change'}
        ],
        stmParamType: [
          {required: true, message: '请输入系统编码', trigger: 'blur'},
          {min: 2, max: 60, message: '长度在2到 60 个字符', trigger: 'blur'}
        ]
      }
    };
  },

  methods: {
    // 单 条停用
    handleStop (index, row) {
      this.$confirm('确认停用该系统配置吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let status = 111002002;
        batchStart(row.id, status).then((res) => {
          this.listLoading = false;
          this.$message({
            message: '停用成功！',
            type: 'success'
          });
          this.searchStmParam();
        });
      }).catch(() => {
      });
    },
    // 单条启用
    handleStart (index, row) {
      this.$confirm('确认启用该系统配置吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let status = 111002001;
        batchStart(row.id, status).then((res) => {
          this.listLoading = false;
          this.$message({
            message: '启用成功！',
            type: 'success'
          });
          this.searchStmParam();
        });
      }).catch(() => {
      });
    },
    //  搜索 栏重置
    myReset () {
      this.searchStmParamType = '';
      this.searchStmParamCnName = '';
      this.searchStmParamVal = '';
    },
    //  搜索栏搜索
    searchStmParam () {
      let filter = '';
      if (this.searchStmParamCnName !== '') {
        filter += 'LIKE_stmParamCnName=' + this.searchStmParamCnName + ';';
      }
      if (this.searchStmParamType !== '') {
        filter += 'LIKE_stmParamType=' + this.searchStmParamType + ';';
      }
      if (this.searchStmParamVal !== '') {
        filter += 'LIKE_stmParamVal=' + this.searchStmParamVal + ';';
      }
      this.listLoading = true;
      searchStmParam({
        filters: filter,
        size: this.pageSize,
        page: this.currentPage - 1,
        sort: (this.sortProp === null ? '' : this.sortProp + (this.sortOrder === 'ascending' ? ',desc' : ',asc'))
      }).then(res => {
        if (res.data) {
          this.tableData = res.data;
          this.total = Number(res.headers['x-total-count']);
          this.listLoading = false;
        }
      });
    },
    // 删除单条数据操作
    handleDel (index, row) {
      console.log(row);
      if (row.status === 111002001) {
        return this.$message.error('已启用，无法删除！');
      }
      this.$confirm('确认删除该系统配置吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        delStmParam(row.id).then((res) => {
          this.listLoading = false;
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.currentPage = 1;
          this.searchStmParam();
        });
      }).catch(() => {
      });
    },
    // 打开编辑界面
    getEditBYId (index, row) {
      console.log(row);
      if (row.status === 111002001) {
        return this.$message.error('已启用，无法编辑！');
      }
      this.stmParam.organizationId1 = [];
      this.resetForm();
      this.stmParam = Object.assign({}, row);
      this.stmParam.organizationId1 = this.stmParam.organizationId.split(',');
      this.dialogTitle = '修改';
      this.stmParamVisible = true;
    },
    // 弹出添加窗体
    handleAdd () {
      this.currentOrganizationIds = [];
      this.dialogTitle = '新增';
      this.resetForm();
      this.stmParamVisible = true;
    },
    // 重置表单
    resetForm () {
      if (this.$refs.stmParam) {
        this.$refs.stmParam.resetFields();
      }
      // 当先点击修改，再点击新增，会出现值没有清空,修复处理
      for (let p in this.stmParam) {
        if (this.stmParam[p] instanceof Array) {
          this.stmParam[p] = [];
        } else {
          this.stmParam[p] = null;
        }
      }
    },

    // 多选操作
    selsChange (sels) {
      this.sels = sels;
    },
    // 排序操作
    sortChange (o) {
      this.sortProp = o.prop;
      this.sortOrder = o.order;
      this.searchStmParam();
    },
    // 每页几条操作
    handleSizeChange (val) {
      this.pageSize = val;
      this.searchStmParam();
    },
    // 第几页操作
    handleCurrentChange (val) {
      this.currentPage = val;
      this.searchStmParam();
    },
    scopeChange () {
      this.stmParam.organizationId1 = [];
      this.stmParam.organizationId = '';
    },
    // 新增数据提交
    handleSubmit () {
      // 表单验证
      this.$refs['stmParam'].validate((valid) => {
        if (valid) {
          if (this.stmParam.organizationId1.length > 0) {
            this.stmParam.organizationId = this.stmParam.organizationId1.join(',');
          }
          if (this.stmParam.scope === 110006001) {
            this.stmParam.organizationId1 = [];
            this.stmParam.organizationId = '';
          }
          this.submitLoading = true;
          saveStmParam(this.stmParam).then((res) => {
            this.submitLoading = false;
            if (res.data) {
              this.stmParamVisible = false;
              this.searchStmParam();
              this.$message.success('保存/修改成功！');
            }
          }).catch(function (error) {
            console.log(error);
          });
        } else {
          return false;
        }
      });
    },

    // 获取表格数据
    getTableData () {
      this.listLoading = true;
      searchStmParam({
        filters: '',
        sort: '',
        page: this.currentPage - 1,
        size: this.pageSize
      }).then((res) => {
        if (res.data) {
          this.tableData = res.data;
          this.total = Number(res.headers['x-total-count']);
          this.listLoading = false;
        }
      });
    }

  },
  // 初始化数据
  mounted () {
    this.getTableData();
    getOrganizationVoById(this.$store.state.currentOrganizationId).then(res => {
      if (res.data) {
        let ids = Object.assign([], res.data.pids);
        ids.push(res.data.id);
        this.currentOrganizationIds = Object.assign([], ids);
      } else {
        this.currentOrganizationIds = [];
      }
    });
    this.$store.commit('initDict');
    this.$store.commit('initOrganizations');
  }
};
</script>
<style lang="less"></style>
